package com.aitesting.controller;


import com.aitesting.APIRunner;
import com.aitesting.common.CommonResult;
import com.aitesting.component.RequestFormConverter;
import com.aitesting.domain.ApiRequestData;
import com.aitesting.form.RequestForm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 测试用例执行
 * @author zz
 * @since 2022-02-20
 */
@Slf4j
@RestController
@RequestMapping("/executor")
public class ExecutorController {

    @RequestMapping(value = "/api", method = RequestMethod.POST)
    public CommonResult execute(@RequestBody RequestForm requestForm){
        //使用MapStruct实现将RequestForm对象数据映射到ApiRequestData对象数据
        ApiRequestData apiRequestData = RequestFormConverter.INSTANCE.requestFormToApiRequestData(requestForm);

        APIRunner runner = new APIRunner();
        List<ApiRequestData> apiRequestDatas = new ArrayList<>();
        apiRequestDatas.add(apiRequestData);
        runner.run(apiRequestDatas);
        return CommonResult.success(runner.getStep_data());
    }
}
